<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="csrf-token" content="{{ csrf_token() }}" />
    <title>layout 后台大布局 - Layui</title>
    <link rel="stylesheet" href="/layui/css/layui.css">

</head>
<body class="layui-layout-body">
<div class="layui-body">
        <!-- 内容主体区域 -->
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>房源添加</legend>
        </fieldset>
        <form class="layui-form layui-form-pane">

            <div class="layui-form-item">
                <label class="layui-form-label">房源信息</label>
                <div class="layui-input-inline">
                    <select name="house_region" id="house_region">
                        <option value="">请选择地区</option>
                        <option value="浙江">浙江省</option>
                        <option value="江西省">江西省</option>
                        <option value="福建省">福建省</option>
                    </select>
                </div>
                <div class="layui-input-inline">
                    <select name="house_address" id="house_address">
                        <option value="">请选择地址</option>
                        <option value="杭州">杭州</option>
                        <option value="温州">温州</option>
                        <option value="台州">台州</option>
                        <option value="绍兴">绍兴</option>
                    </select>
                </div>
                <div class="layui-input-inline">
                    <select name="house_unit_num" id="house_unit_num">
                        <option value="">请选择单元数</option>
                        <option value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                    </select>
                </div>
                <div class="layui-input-inline">
                    <select name="house_layer_num" id="house_layer_num">
                        <option value="">请选择层数</option>
                        <option value="10">10</option>
                        <option value="20">20</option>
                        <option value="30">30</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item" pane="">
                <label class="layui-form-label">房源配置</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="is_television" id="is_television" lay-skin="primary" title="是否配置电视">
                    <input type="checkbox" name="is_air_conditioning" id="is_air_conditioning" lay-skin="primary" title="是否配置空调">
                    <input type="checkbox" name="is_refrigerator" id="is_refrigerator" lay-skin="primary" title="是否配置冰箱">
                </div>
            </div>
            <div class="layui-upload">
                <button type="button" class="layui-btn layui-btn-normal" id="testList">选择多文件</button>
                <div class="layui-upload-list">
                    <table class="layui-table">
                        <thead>
                        <tr><th>文件名</th>
                            <th>大小</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr></thead>
                        <tbody id="demoList"></tbody>
                    </table>
                </div>
                <button type="button" class="layui-btn" id="testListAction">开始上传</button>
            </div>
            <div class="layui-upload">
                <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                    预览图：
                    <div class="layui-upload-list" id="demo2"></div>
                </blockquote>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">小区名称</label>
                <div class="layui-input-inline">
                    <input type="hidden" id="_token" name="_token" value="{{ csrf_token() }}">
                    <input name="house_village_name" id="house_village_name" class="layui-input" type="text" placeholder="请输入" autocomplete="off" lay-verify="required">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">房间号码</label>
                <div class="layui-input-inline">
                    <input name="house_room" id="house_room" class="layui-input" type="text" placeholder="请输入" autocomplete="off" lay-verify="required">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">房源平米数</label>
                <div class="layui-input-inline">
                    <input name="house_size" id="house_size" class="layui-input" type="text" placeholder="请输入" autocomplete="off" lay-verify="required">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">房源价格</label>
                    <div class="layui-input-inline" style="width: 100px;">
                        <input type="text" name="house_price" id="house_price" placeholder="￥" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">配置销售员</label>
                <div class="layui-input-block">
                    <select name="sale_id" id="sale_id" lay-filter="aihao">
                        <@foreach($data as $v)
                            <option value="{{$v->sale_id}}">{{$v->sale_user}}</option>
                        @endforeach
                    </select>
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">房屋介绍</label>
                <div class="layui-input-block">
                    <textarea placeholder="请输入内容" name="house_text" id="house_text" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-input-block">
                <button  class="layui-btn" lay-submit id="tweets_sub"  lay-filter="tweets_sub">提交</button>
                <button type="reset" class="layui-btn-primary layui-btn">重置</button>
            </div>
        </form>
    </div>
</body>
<script src="/layui/layui.js"></script>
<script src="/js/jq.js"></script>
<script>
    layui.use('upload', function(){
        var $ = layui.jquery
            ,upload = layui.upload;
        //多文件列表示例
        var demoListView = $('#demoList')
            ,uploadListIns = upload.render({
            elem: '#testList'
            ,url: '/admin/upload'
            ,field:'photo'
            ,multiple: true
            ,auto: false
            ,headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
            }
            ,bindAction: '#testListAction'
            ,choose: function(obj){
                var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                    //读取本地文件
                obj.preview(function(index, file, result){
                    var tr = $(['<tr id="upload-'+ index +'">'
                        ,'<td>'+ file.name +'</td>'
                        ,'<td>'+ (file.size/1014).toFixed(1) +'kb</td>'
                        ,'<td>等待上传</td>'
                        ,'<td>'
                        ,'<button class="layui-btn layui-btn-mini demo-reload layui-hide">重传</button>'
                        ,'<button class="layui-btn layui-btn-mini layui-btn-danger demo-delete">删除</button>'
                        ,'</td>'
                        ,'</tr>'].join(''));

                    //单个重传
                    tr.find('.demo-reload').on('click', function(){
                        obj.upload(index, file);
                    });

                    //删除
                    tr.find('.demo-delete').on('click', function(){
                        delete files[index]; //删除对应的文件
                        tr.remove();
                        uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                    });

                    demoListView.append(tr);
                });
            }
            ,done: function(res, index, upload){
                console.log(res.ResultData);
                var img = res.ResultData;
                var _token = $('#_token').val();
                var data = new FormData();
                data.append('img',img);
                data.append('_token',_token);
                if(res.code == 0){ //上传成功
                    $.ajax({
                        type:"post",
                        url:"{{url('/admin/house_add_do')}}",
                        data:data,
                        cache: false,
                        processData: false,
                        contentType: false,
                    });
                    $('#demo2').append('<img src="/uploads/'+ res.ResultData +'" alt="'+ res.ResultData +'" value="'+ res.ResultData +'" class="layui-upload-img" name="house_img_one" id="house_img_one">')
                    var tr = demoListView.find('tr#upload-'+ index)
                        ,tds = tr.children();
                    tds.eq(2).html('<span style="color: #5FB878;">成功</span>');
                    tds.eq(3).html(''); //清空操作
                    return delete this.files[index]; //删除文件队列已经上传成功的文件
                }
                this.error(index, upload);
            }
            ,error: function(index, upload){
                var tr = demoListView.find('tr#upload-'+ index)
                    ,tds = tr.children();
                tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
                tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
            }
        });
    });
    layui.use('form', function(){
        var form = layui.form;
        form.on('submit(tweets_sub)', function(){
            var house_room = $('#house_room').val();
            var house_region = $('#house_region').val();
            var house_village_name = $('#house_village_name').val();
            var house_address = $('#house_address').val();
            var house_unit_num = $('#house_unit_num').val();
            var house_layer_num = $('#house_layer_num').val();
            var house_price = $('#house_price').val();
            var house_size = $('#house_size').val();
            var is_television = $('#is_television').val();
            var is_air_conditioning = $('#is_air_conditioning').val();
            var is_refrigerator = $('#is_refrigerator').val();
            var house_text = $('#house_text').val();
            var sale_id = $('#sale_id').val();
            var house_img_one = $("#house_img_one")[0].src;
            var _token = $('#_token').val();
            var data = new FormData();
            data.append('house_room',house_room);
            data.append('house_region',house_region);
            data.append('house_village_name',house_village_name);
            data.append('house_address',house_address);
            data.append('house_unit_num',house_unit_num);
            data.append('house_layer_num',house_layer_num);
            data.append('house_price',house_price);
            data.append('house_size',house_size);
            data.append('is_television',is_television);
            data.append('is_air_conditioning',is_air_conditioning);
            data.append('is_refrigerator',is_refrigerator);
            data.append('house_text',house_text);
            data.append('sale_id',sale_id);
            data.append('house_img_one',house_img_one);
            data.append('_token',_token);
            $.ajax({
                type:"post",
                url:"{{url('/admin/house_add_do')}}",
                data:data,
                cache: false,
                processData: false,
                contentType: false,
                success:function(msg){
                    if(msg==1){
                        window.location.href = 'http://www.cucurbit.com/index/house';//登录成功跳转
                    }else{
                        parent.location.reload();
                    }
                },
            });
            return false;
        });
    });

</script>
</body>
</html>